package sg.edu.nus.iss.pacman.manager.dao;

import java.util.Vector;

import sg.edu.nus.iss.pacman.dao.RmsUserProfileDao;
import sg.edu.nus.iss.pacman.dao.Score;
import sg.edu.nus.iss.pacman.dao.TopScoreUser;
import sg.edu.nus.iss.pacman.dao.UserProfile;
import sg.edu.nus.iss.pacman.dao.UserProfileDao;

/**
 * Manager class to access User Profile
 * 
 * @author Daniel, fredy
 */
public class UserProfileManager {
	
	private UserProfileDao userDao;

	/**
	 * Constructor
	 */
	public UserProfileManager() {
		userDao = new RmsUserProfileDao();
	}
	
	/**
	 * Gets list of 5 top scorers.
	 *
     * @param mazeName
     * @return list of 5 top scorers
	 */
	public TopScoreUser[] getListOfTopScorers(String mazeName) {
		TopScoreUser[] result = new TopScoreUser[0];
		
		UserProfile[] dataUser = userDao.getUserProfiles();
		
		//Get all users who have played a particular maze
		Vector tempUserVector = new Vector();
		
		if (dataUser != null && dataUser.length > 0) {
			int length = dataUser.length;
			
			for (int i=0; i<length; i++) {
				UserProfile user = dataUser[i];				
				Vector scores = user.getScores();
				
				if (scores != null && scores.size() > 0) {
					for (int j=0; j<scores.size(); j++) {
						Score valScore = (Score) scores.elementAt(j);
						
						if (valScore.getMapName().equals(mazeName)) {
							tempUserVector.addElement(new TopScoreUser(user.getName(), 
									valScore.getMapName(), valScore.getScore()));
							break;
						}
					}
				}
			}
		}
		
		
		if (tempUserVector.size() > 0) {
			int length = tempUserVector.size();
			result = new TopScoreUser[length];
			for (int i=0; i<length; i++) {
				result[i] = (TopScoreUser) tempUserVector.elementAt(i);
			}
			
			//sorting
			for (int i=0; i<length; i++) {
				TopScoreUser user1 = result[i];
				for (int j=i+1; j<length; j++) {
					TopScoreUser user2 = result[j];
					if (user1.getScore() < user2.getScore()) {
						TopScoreUser userTemp = user1;
						result[i] = user2;
						result[j] = userTemp;
						user1 = user2;
					}
				}
			}
		}
		
		return result;
	}
	
	/**
	 * Saves user profile
	 * @param userProfile user profile
	 * @return the number of rows updated.
	 */
	public int saveUserProfile(UserProfile userProfile) {
		UserProfile oldUser = getUserProfile(userProfile.getName());
		
		if (oldUser != null) {
			deleteUserProfile(userProfile.getName());	
		}
				
		return userDao.saveUserProfile(userProfile);
	}
	
	/**
	 * Deletes user profile
     * @param name the name
	 */
	public void deleteUserProfile(String name) {
		userDao.deleteUserProfile(name);
	}
	
	/**
	 * Gets user profile based on user id
     * @param name the name
     * @return user profile
	 */
	public UserProfile getUserProfile(String name) {
		return userDao.findUserProfile(name);
	}
	
	/**
	 * Gets list of all user profiles
	 * @return list of all user profiles
	 */
	public UserProfile[] getListUserProfiles() {
		return userDao.getUserProfiles();
	}
	
	
}
